package com.lgd.factoryattendance.httpService;


import android.util.Log;

import com.google.gson.Gson;
import com.lgd.factoryattendance.Bean.NormalResponse;
import com.lgd.factoryattendance.Utils.ExceptionUtil;
import com.lgd.factoryattendance.Utils.StringUtil;
import com.lgd.factoryattendance.httpService.httpPort.IHttpHelper;
import com.lgd.factoryattendance.httpService.httpPort.INpResponse;
import com.lgd.factoryattendance.httpService.httpPort.IResponse;
import com.lgd.factoryattendance.httpService.httpPort.IStringResponse;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class HttpHelperImpl implements IHttpHelper {

    @Override
    public void get(String url, IResponse response) {
        try {
            TsHttp.getInstance().get(url,response);
        }catch (Exception e){

        }
    }

//    @Override
//    public void getB(String url, Object obj, IStringResponse response) {
//        try{
//            String json="";
//            if(null!=obj) json=new Gson().toJson(obj);
////            TsHttp.getInstance().post(url,json,response);
//        }catch (Exception e){
//            response.onFail(-2);
//        }
//    }

    /**
     * statusCode
     * -2
     * -1
     * 0
     * 200
     * 400
     * 500
     * @param url
     * @param map
     * @param response
     */

    @Override
    public void get(String url, Map<String, Object> map, IStringResponse response) {
        try{
            String params="?";
            if(null!=map && map.size()>0){
                for(String key : map.keySet()){
                    Object obj=map.get(key);
                    if(null==obj)obj="";
                    String value=obj.toString();
                    value= URLEncoder.encode(value,"utf-8");
                    params=params+key+"="+value+"&";
                }
                if(params.endsWith("&"))params=params.substring(0,params.length()-1);
            }
            if("?".equals(params))params="";
            url=url+params;
            TsHttp.getInstance().get(url,response);
        }catch (Exception e){
            response.onFail(-2);
        }
    }

    @Override
    public void post(String url, Object obj, IStringResponse response) {
       try{
           String json="";
           if(null!=obj) json=new Gson().toJson(obj);
           TsHttp.getInstance().post(url,json,response);
       }catch (Exception e){
           response.onFail(-2);
       }
    }

    @Override
    public void getnp(String url, Map<String, Object> map,final INpResponse response) {
       this.get(url, map, new IStringResponse() {
           @Override
           public void onSuccess(String result) {
                if(StringUtil.isNullOrEmpty(response)){
                    response.onSuccess(new NormalResponse(false,""));
                }else{
                    try{
                        NormalResponse np=new Gson().fromJson(result, NormalResponse.class);
                        response.onSuccess(np);
                    }catch (Exception e){
                        response.onSuccess(new NormalResponse(false,"返回格式非NormalResponse","",result));
                    }
                }
           }

           @Override
           public void onFail(int statusCode) {
               response.onFail(statusCode);
           }
       });
    }

    @Override
    public void postnp(String url, Object obj,final INpResponse response) {
        this.post(url, obj, new IStringResponse() {
            @Override
            public void onSuccess(String result) {
                if(StringUtil.isNullOrEmpty(response)){
                    response.onSuccess(new NormalResponse(false,""));
                }else{
                    try{
                        NormalResponse np=new Gson().fromJson(result,NormalResponse.class);
                        response.onSuccess(np);
                    }catch (Exception e){
                        ExceptionUtil.getStackTraceInfo(e);
                        response.onSuccess(new NormalResponse(false,"返回格式非NormalResponse","",result));
                    }
                }
            }

            @Override
            public void onFail(int statusCode) {
                response.onFail(statusCode);
            }
        });
    }

    @Override
    public void Fife(String url, String path, IStringResponse myResponse) {

        System.out.println(path+"00000000000");
        OkHttpClient okHttpClient = new OkHttpClient();
        File file = new File(path);
        String filename = path.substring(path.lastIndexOf("/") + 1);

        RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addPart(Headers.of(
                        "Content-Disposition",
                        "form-data; name=\"getUpTime\""),
                        RequestBody.create(null, "2019-3-29"))
                .addPart(Headers.of(
                        "Content-Disposition",
                        "form-data; name=\"file\"; filename=\"" + filename + "\""), fileBody)
                .build();
        System.out.println("----------------------------------------------------");
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        System.out.println(requestBody+"//////////");
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                myResponse.onFail(0);
                Log.d("RequestFF","请求失败" + url);
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = null;
                    try {
                        result = response.body().string();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    myResponse.onSuccess(result);
                }else{
                    myResponse.onFail(response.code());
                }
            }
        });
    }

}
